{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bb083515-b8b3-440c-8799-0d929b767fc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"NEO4J_URI\"] = 'bolt://localhost:7687'\n",
    "os.environ[\"NEO4J_USERNAME\"] = 'neo4j'\n",
    "os.environ[\"NEO4J_PASSWORD\"] = 'password'\n",
    "os.environ[\"OPENAI_API_KEY\"] = 'sk-'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "96243fa0-d295-4f3c-b98f-9595b798fe25",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/tomazbratanic/anaconda3/lib/python3.11/site-packages/pandas/core/arrays/masked.py:60: UserWarning: Pandas requires version '1.3.6' or newer of 'bottleneck' (version '1.3.5' currently installed).\n",
      "  from pandas.core import (\n"
     ]
    }
   ],
   "source": [
    "import dspy\n",
    "from dspy.retrieve.neo4j_rm import Neo4jRM\n",
    "\n",
    "# Configure LLM\n",
    "lm = dspy.OpenAI(model=\"gpt-3.5-turbo\")\n",
    "\n",
    "# Configure Retriever\n",
    "rm = Neo4jRM(\n",
    "    index_name=\"vector\",\n",
    "    text_node_property=\"text\"\n",
    ")\n",
    "\n",
    "# Configure DSPy to use the following language model and retrieval model by default\n",
    "dspy.settings.configure(lm = lm, \n",
    "                        rm = rm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6c7d6692-a7a0-4c63-a05f-cf3d83c3ce06",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Small training set with question and answer pairs\n",
    "trainset = [dspy.Example(question=\"What were the two main things the author worked on before college?\", \n",
    "                         answer=\"Writing and programming\").with_inputs('question'),\n",
    "            dspy.Example(question=\"What kind of writing did the author do before college?\", \n",
    "                         answer=\"Short stories\").with_inputs('question'),\n",
    "            dspy.Example(question=\"What was the first computer language the author learned?\", \n",
    "                         answer=\"Fortran\").with_inputs('question'),\n",
    "            dspy.Example(question=\"What kind of computer did the author's father buy?\", \n",
    "                         answer=\"TRS-80\").with_inputs('question'),\n",
    "            dspy.Example(question=\"What was the author's original plan for college?\", \n",
    "                         answer=\"Study philosophy\").with_inputs('question'),]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6953d350-c919-40e1-917c-1ea6c54735a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "class GenerateAnswer(dspy.Signature):\n",
    "    \"\"\"Answer questions with short factoid answers.\"\"\"\n",
    "\n",
    "    context = dspy.InputField(desc=\"may contain relevant facts\")\n",
    "    question = dspy.InputField()\n",
    "    answer = dspy.OutputField(desc=\"often between 1 and 5 words\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "02f24a67-67b3-40a0-b5c3-2c0874c75e64",
   "metadata": {},
   "outputs": [],
   "source": [
    "class RAG(dspy.Module):\n",
    "    def __init__(self, num_passages=3):\n",
    "        super().__init__()\n",
    "\n",
    "        self.retrieve = dspy.Retrieve(k=num_passages)\n",
    "        self.generate_answer = dspy.ChainOfThought(GenerateAnswer)\n",
    "    \n",
    "    def forward(self, question):\n",
    "        context = self.retrieve(question).passages\n",
    "        prediction = self.generate_answer(context=context, question=question)\n",
    "        return dspy.Prediction(context=context, answer=prediction.answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "72f59e74-394d-470a-bb7b-e66aced0c75f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5/5 [00:01<00:00,  3.30it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bootstrapped 1 full traces after 5 examples in round 0.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from dspy.teleprompt import BootstrapFewShot\n",
    "\n",
    "# Set up a basic teleprompter, which will compile our RAG program.\n",
    "teleprompter = BootstrapFewShot(metric=dspy.evaluate.answer_exact_match)\n",
    "\n",
    "# Compile!\n",
    "compiled_rag = teleprompter.compile(RAG(), trainset=trainset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9df6021c-77ac-4a18-ad24-5e48ae73b830",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred = compiled_rag(question = \"Who is Ada Lovelace?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "db55d200-a5a7-4d0b-8be9-43f214e53f77",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Prediction(\n",
       "    context=['Augusta Ada King, Countess of Lovelace (née Byron; 10 December 1815 – 27 November 1852) was an English mathematician and writer, chiefly known for her work on Charles Babbage\\'s proposed mechanical general-purpose computer, the Analytical Engine. She was the first to recognise that the machine had applications beyond pure calculation.\\nAda Byron was the only legitimate child of poet Lord Byron and reformer Anne Isabella Milbanke. All Lovelace\\'s half-siblings, Lord Byron\\'s other children, were born out of wedlock to other women. Byron separated from his wife a month after Ada was born and left England forever.  He died in Greece when Ada was eight. Her mother was anxious about her upbringing and promoted Ada\\'s interest in mathematics and logic in an effort to prevent her from developing her father\\'s perceived insanity. Despite this, Ada remained interested in him, naming her two sons Byron and Gordon. Upon her death, she was buried next to him at her request. Although often ill in her childhood, Ada pursued her studies assiduously. She married William King in 1835. King was made Earl of Lovelace in 1838, Ada thereby becoming Countess of Lovelace.\\nHer educational and social exploits brought her into contact with scientists such as Andrew Crosse, Charles Babbage, Sir David Brewster, Charles Wheatstone, Michael Faraday, and the author Charles Dickens, contacts which she used to further her education. Ada described her approach as \"poetical science\" and herself as an \"Analyst (& Metaphysician)\".When she was eighteen, her mathematical talents led her to a long working relationship and friendship with fellow British mathematician Charles Babbage, who is known as \"the father of computers\". She was in particular interested in Babbage\\'s work on the Analytical Engine. Lovelace first met him on 5 June 1833, when she and her mother attended one of Charles Babbage\\'s Saturday night soirées with their mutual friend, and Ada\\'s private tutor, Mary Somerville.\\nBetween 1842 and 1843, Ada translated an article by the military engineer Luigi Menabrea (later Prime Minister of Italy) about the Analytical Engine, supplementing it with an elaborate set of seven notes, simply called \"Notes\".\\nLovelace\\'s notes are important in the early history of computers, especially since the seventh one contained what many consider to be the first computer program—that is, an algorithm designed to be carried out', ' the seventh one contained what many consider to be the first computer program—that is, an algorithm designed to be carried out by a machine. Other historians reject this perspective and point out that Babbage\\'s personal notes from the years 1836/1837 contain the first programs for the engine. She also developed a vision of the capability of computers to go beyond mere calculating or number-crunching, while many others, including Babbage himself, focused only on those capabilities. Her mindset of \"poetical science\" led her to ask questions about the Analytical Engine (as shown in her notes) examining how individuals and society relate to technology as a collaborative tool.\\n\\n\\n== Biography ==\\n\\n\\n=== Childhood ===\\nLord Byron expected his child to be a \"glorious boy\" and was disappointed when Lady Byron gave birth to a girl. The child was named after Byron\\'s half-sister, Augusta Leigh, and was called \"Ada\" by Byron himself. On 16 January 1816, at Lord Byron\\'s command, Lady Byron left for her parents\\' home at Kirkby Mallory, taking their five-week-old daughter with her. Although English law at the time granted full custody of children to the father in cases of separation, Lord Byron made no attempt to claim his parental rights, but did request that his sister keep him informed of Ada\\'s welfare.\\nOn 21 April, Lord Byron signed the deed of separation, although very reluctantly, and left England for good a few days later. Aside from an acrimonious separation, Lady Byron continued throughout her life to make allegations about her husband\\'s immoral behaviour. This set of events made Lovelace infamous in Victorian society. Ada did not have a relationship with her father. He died in 1824 when she was ', 'Ada Lovelace Day is an annual event held on the second Tuesday of October to celebrate and raise awareness of the contributions of women to STEM fields. It is named after mathematician and computer science pioneer Ada Lovelace. It started in 2009 as a \"day of blogging\" and has since become a multi-national event with conferences.\\n\\n\\n== History ==\\nThe day was founded in the United Kingdom in 2009 by Suw Charman-Anderson on the second Tuesday in October as a means of raising awareness about the contributions of women to science, technology, engineering, and mathematics (STEM) fields.In 2022, Charman-Anderson announced that this would be the last year in which the organization that she founded, Finding Ada, would organize an annual flagship Ada Lovelace Day event in England.Since its inception, Ada Lovelace Day has become international in scope, with events organized by groups ranging from museums, professional societies, universities, colleges and high schools. While Ada Lovelace Day is the second Tuesday of October, events celebrating women in STEM typically span the period of October and November, and include diverse activities ranging from in-person and virtual Wikipedia Editathons to panel discussions and film screenings.\\n\\nWhile this celebration of the often overlooked contributions of women in STEM was named for Ada Lovelace, activities have expanded since 2009 to highlight the diverse contributions of women in STEM over time and different countries. Events have featured policy initiatives and scholarship relating to equity, diversity and inclusion that provide spaces and platforms for dialogue and discussion about how unconscious bias(es) function to create barriers to women\\'s participation and advancement in the professional fields of STEM.\\n\\n\\n== References ==\\n\\n\\n== External links ==\\nOfficial website'],\n",
       "    answer='English mathematician and writer'\n",
       ")"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "643b8d00-0cc3-468f-bb9a-6306d09544af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "\n",
      "Answer questions with short factoid answers.\n",
      "\n",
      "---\n",
      "\n",
      "Question: What was the author's original plan for college?\n",
      "Answer: Study philosophy\n",
      "\n",
      "Question: What were the two main things the author worked on before college?\n",
      "Answer: Writing and programming\n",
      "\n",
      "Question: What kind of computer did the author's father buy?\n",
      "Answer: TRS-80\n",
      "\n",
      "Question: What kind of writing did the author do before college?\n",
      "Answer: Short stories\n",
      "\n",
      "---\n",
      "\n",
      "Follow the following format.\n",
      "\n",
      "Context: may contain relevant facts\n",
      "\n",
      "Question: ${question}\n",
      "\n",
      "Reasoning: Let's think step by step in order to ${produce the answer}. We ...\n",
      "\n",
      "Answer: often between 1 and 5 words\n",
      "\n",
      "---\n",
      "\n",
      "Context:\n",
      "[1] « the seventh one contained what many consider to be the first computer program—that is, an algorithm designed to be carried out by a machine. Other historians reject this perspective and point out that Babbage's personal notes from the years 1836/1837 contain the first programs for the engine. She also developed a vision of the capability of computers to go beyond mere calculating or number-crunching, while many others, including Babbage himself, focused only on those capabilities. Her mindset of \"poetical science\" led her to ask questions about the Analytical Engine (as shown in her notes) examining how individuals and society relate to technology as a collaborative tool.\n",
      "\n",
      "\n",
      "== Biography ==\n",
      "\n",
      "\n",
      "=== Childhood ===\n",
      "Lord Byron expected his child to be a \"glorious boy\" and was disappointed when Lady Byron gave birth to a girl. The child was named after Byron's half-sister, Augusta Leigh, and was called \"Ada\" by Byron himself. On 16 January 1816, at Lord Byron's command, Lady Byron left for her parents' home at Kirkby Mallory, taking their five-week-old daughter with her. Although English law at the time granted full custody of children to the father in cases of separation, Lord Byron made no attempt to claim his parental rights, but did request that his sister keep him informed of Ada's welfare.\n",
      "On 21 April, Lord Byron signed the deed of separation, although very reluctantly, and left England for good a few days later. Aside from an acrimonious separation, Lady Byron continued throughout her life to make allegations about her husband's immoral behaviour. This set of events made Lovelace infamous in Victorian society. Ada did not have a relationship with her father. He died in 1824 when she was »\n",
      "[2] «Augusta Ada King, Countess of Lovelace (née Byron; 10 December 1815 – 27 November 1852) was an English mathematician and writer, chiefly known for her work on Charles Babbage's proposed mechanical general-purpose computer, the Analytical Engine. She was the first to recognise that the machine had applications beyond pure calculation.\n",
      "Ada Byron was the only legitimate child of poet Lord Byron and reformer Anne Isabella Milbanke. All Lovelace's half-siblings, Lord Byron's other children, were born out of wedlock to other women. Byron separated from his wife a month after Ada was born and left England forever.  He died in Greece when Ada was eight. Her mother was anxious about her upbringing and promoted Ada's interest in mathematics and logic in an effort to prevent her from developing her father's perceived insanity. Despite this, Ada remained interested in him, naming her two sons Byron and Gordon. Upon her death, she was buried next to him at her request. Although often ill in her childhood, Ada pursued her studies assiduously. She married William King in 1835. King was made Earl of Lovelace in 1838, Ada thereby becoming Countess of Lovelace.\n",
      "Her educational and social exploits brought her into contact with scientists such as Andrew Crosse, Charles Babbage, Sir David Brewster, Charles Wheatstone, Michael Faraday, and the author Charles Dickens, contacts which she used to further her education. Ada described her approach as \"poetical science\" and herself as an \"Analyst (& Metaphysician)\".When she was eighteen, her mathematical talents led her to a long working relationship and friendship with fellow British mathematician Charles Babbage, who is known as \"the father of computers\". She was in particular interested in Babbage's work on the Analytical Engine. Lovelace first met him on 5 June 1833, when she and her mother attended one of Charles Babbage's Saturday night soirées with their mutual friend, and Ada's private tutor, Mary Somerville.\n",
      "Between 1842 and 1843, Ada translated an article by the military engineer Luigi Menabrea (later Prime Minister of Italy) about the Analytical Engine, supplementing it with an elaborate set of seven notes, simply called \"Notes\".\n",
      "Lovelace's notes are important in the early history of computers, especially since the seventh one contained what many consider to be the first computer program—that is, an algorithm designed to be carried out»\n",
      "[3] «Ada Lovelace Day is an annual event held on the second Tuesday of October to celebrate and raise awareness of the contributions of women to STEM fields. It is named after mathematician and computer science pioneer Ada Lovelace. It started in 2009 as a \"day of blogging\" and has since become a multi-national event with conferences.\n",
      "\n",
      "\n",
      "== History ==\n",
      "The day was founded in the United Kingdom in 2009 by Suw Charman-Anderson on the second Tuesday in October as a means of raising awareness about the contributions of women to science, technology, engineering, and mathematics (STEM) fields.In 2022, Charman-Anderson announced that this would be the last year in which the organization that she founded, Finding Ada, would organize an annual flagship Ada Lovelace Day event in England.Since its inception, Ada Lovelace Day has become international in scope, with events organized by groups ranging from museums, professional societies, universities, colleges and high schools. While Ada Lovelace Day is the second Tuesday of October, events celebrating women in STEM typically span the period of October and November, and include diverse activities ranging from in-person and virtual Wikipedia Editathons to panel discussions and film screenings.\n",
      "\n",
      "While this celebration of the often overlooked contributions of women in STEM was named for Ada Lovelace, activities have expanded since 2009 to highlight the diverse contributions of women in STEM over time and different countries. Events have featured policy initiatives and scholarship relating to equity, diversity and inclusion that provide spaces and platforms for dialogue and discussion about how unconscious bias(es) function to create barriers to women's participation and advancement in the professional fields of STEM.\n",
      "\n",
      "\n",
      "== References ==\n",
      "\n",
      "\n",
      "== External links ==\n",
      "Official website»\n",
      "\n",
      "Question: What was the first computer language the author learned?\n",
      "\n",
      "Reasoning: Let's think step by step in order to Answer: Fortran\n",
      "\n",
      "Answer: Fortran\n",
      "\n",
      "---\n",
      "\n",
      "Context:\n",
      "[1] «Augusta Ada King, Countess of Lovelace (née Byron; 10 December 1815 – 27 November 1852) was an English mathematician and writer, chiefly known for her work on Charles Babbage's proposed mechanical general-purpose computer, the Analytical Engine. She was the first to recognise that the machine had applications beyond pure calculation.\n",
      "Ada Byron was the only legitimate child of poet Lord Byron and reformer Anne Isabella Milbanke. All Lovelace's half-siblings, Lord Byron's other children, were born out of wedlock to other women. Byron separated from his wife a month after Ada was born and left England forever.  He died in Greece when Ada was eight. Her mother was anxious about her upbringing and promoted Ada's interest in mathematics and logic in an effort to prevent her from developing her father's perceived insanity. Despite this, Ada remained interested in him, naming her two sons Byron and Gordon. Upon her death, she was buried next to him at her request. Although often ill in her childhood, Ada pursued her studies assiduously. She married William King in 1835. King was made Earl of Lovelace in 1838, Ada thereby becoming Countess of Lovelace.\n",
      "Her educational and social exploits brought her into contact with scientists such as Andrew Crosse, Charles Babbage, Sir David Brewster, Charles Wheatstone, Michael Faraday, and the author Charles Dickens, contacts which she used to further her education. Ada described her approach as \"poetical science\" and herself as an \"Analyst (& Metaphysician)\".When she was eighteen, her mathematical talents led her to a long working relationship and friendship with fellow British mathematician Charles Babbage, who is known as \"the father of computers\". She was in particular interested in Babbage's work on the Analytical Engine. Lovelace first met him on 5 June 1833, when she and her mother attended one of Charles Babbage's Saturday night soirées with their mutual friend, and Ada's private tutor, Mary Somerville.\n",
      "Between 1842 and 1843, Ada translated an article by the military engineer Luigi Menabrea (later Prime Minister of Italy) about the Analytical Engine, supplementing it with an elaborate set of seven notes, simply called \"Notes\".\n",
      "Lovelace's notes are important in the early history of computers, especially since the seventh one contained what many consider to be the first computer program—that is, an algorithm designed to be carried out»\n",
      "[2] « the seventh one contained what many consider to be the first computer program—that is, an algorithm designed to be carried out by a machine. Other historians reject this perspective and point out that Babbage's personal notes from the years 1836/1837 contain the first programs for the engine. She also developed a vision of the capability of computers to go beyond mere calculating or number-crunching, while many others, including Babbage himself, focused only on those capabilities. Her mindset of \"poetical science\" led her to ask questions about the Analytical Engine (as shown in her notes) examining how individuals and society relate to technology as a collaborative tool.\n",
      "\n",
      "\n",
      "== Biography ==\n",
      "\n",
      "\n",
      "=== Childhood ===\n",
      "Lord Byron expected his child to be a \"glorious boy\" and was disappointed when Lady Byron gave birth to a girl. The child was named after Byron's half-sister, Augusta Leigh, and was called \"Ada\" by Byron himself. On 16 January 1816, at Lord Byron's command, Lady Byron left for her parents' home at Kirkby Mallory, taking their five-week-old daughter with her. Although English law at the time granted full custody of children to the father in cases of separation, Lord Byron made no attempt to claim his parental rights, but did request that his sister keep him informed of Ada's welfare.\n",
      "On 21 April, Lord Byron signed the deed of separation, although very reluctantly, and left England for good a few days later. Aside from an acrimonious separation, Lady Byron continued throughout her life to make allegations about her husband's immoral behaviour. This set of events made Lovelace infamous in Victorian society. Ada did not have a relationship with her father. He died in 1824 when she was »\n",
      "[3] «Ada Lovelace Day is an annual event held on the second Tuesday of October to celebrate and raise awareness of the contributions of women to STEM fields. It is named after mathematician and computer science pioneer Ada Lovelace. It started in 2009 as a \"day of blogging\" and has since become a multi-national event with conferences.\n",
      "\n",
      "\n",
      "== History ==\n",
      "The day was founded in the United Kingdom in 2009 by Suw Charman-Anderson on the second Tuesday in October as a means of raising awareness about the contributions of women to science, technology, engineering, and mathematics (STEM) fields.In 2022, Charman-Anderson announced that this would be the last year in which the organization that she founded, Finding Ada, would organize an annual flagship Ada Lovelace Day event in England.Since its inception, Ada Lovelace Day has become international in scope, with events organized by groups ranging from museums, professional societies, universities, colleges and high schools. While Ada Lovelace Day is the second Tuesday of October, events celebrating women in STEM typically span the period of October and November, and include diverse activities ranging from in-person and virtual Wikipedia Editathons to panel discussions and film screenings.\n",
      "\n",
      "While this celebration of the often overlooked contributions of women in STEM was named for Ada Lovelace, activities have expanded since 2009 to highlight the diverse contributions of women in STEM over time and different countries. Events have featured policy initiatives and scholarship relating to equity, diversity and inclusion that provide spaces and platforms for dialogue and discussion about how unconscious bias(es) function to create barriers to women's participation and advancement in the professional fields of STEM.\n",
      "\n",
      "\n",
      "== References ==\n",
      "\n",
      "\n",
      "== External links ==\n",
      "Official website»\n",
      "\n",
      "Question: Who is Ada Lovelace?\n",
      "\n",
      "Reasoning: Let's think step by step in order to Answer: English mathematician and writer\n",
      "\n",
      "Answer:\u001b[32m English mathematician and writer\u001b[0m\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "lm.inspect_history(n=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45437bfb-03c9-41db-bdd6-d6bd28097e5c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
